﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class ListUser : PageView<ListUserViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }
        string Script1(ListUserViewModel model)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" var pageSize = {0}; ", model.PageSize);
            sb.AppendFormat("var pageCut = {0};", model.PageCute);
            sb.Append("$(document).ready(function () {");
            sb.Append(" displayPage(1); ");
            sb.Append(" });");
            sb.Append(" function displayPage(page) {");
            sb.Append("var recordCount = $(\".tablerow\").length;");
            sb.Append(" var yu = recordCount % pageSize;");
            sb.Append(" var addCount = 0;");
            sb.Append("  if (yu > 0)");
            sb.Append("addCount = 1;");
            sb.Append("var pagecount = parseInt(recordCount / pageSize) + addCount; ");
            sb.Append("var pageLabel = \"\";");
            sb.Append("if (page > 1) {");
            sb.Append("pageLabel += \"<a href='#' title='首页' onclick='displayPage(1);'>&laquo; 首页</a>\";");
            sb.Append("  pageLabel += \"<a href='#' title='上一页' onclick='displayPage(\" + (page - 1).toString() + \");'>&laquo; 上一页</a>\";");
            sb.Append(" }");
            sb.Append("var firstCut = page - pageCut;");
            sb.Append("  if (firstCut < 1)");
            sb.Append(" firstCut = 1;");
            sb.Append("var lastCut = page + pageCut;");
            sb.Append("if (lastCut > pagecount)");
            sb.Append(" lastCut = pagecount;");
            sb.Append(" for (i = firstCut; i <= lastCut; i++) {");
            sb.Append(" if (i == page) {");
            sb.Append(" pageLabel += \"<a id='pageindex\" + i + \"' href='#' class='selectedpagelabel'  title='\" + i + \"'>\" + i + \"</a>&nbsp;&nbsp;\";");
            sb.Append(" } else {");
            sb.Append(" pageLabel += \"<a id='pageindex\" + i + \"' href='#' class='number' onclick='displayPage(\" + i + \");' title='\" + i + \"'>\" + i + \"</a>&nbsp;&nbsp;\";");
            sb.Append(" }   }");
            sb.Append(" if (page < pagecount) {");
            sb.Append("pageLabel += \"<a href='#' title='下一页' onclick='displayPage(\" + (page + 1).toString() + \");'>下一页 &raquo;</a>\";");
            sb.Append("  pageLabel += \"<a href='#' title='末页' onclick='displayPage(\" + pagecount + \");'>末页 &raquo;</a>\";");
            sb.Append("}");
            sb.Append("$(\"#pagelabel\").html(pageLabel);");
            sb.Append("var begin = (page - 1) * pageSize;");
            sb.Append(" var end = page * pageSize; ");
            sb.Append(" $(\".tablerow\").hide();");
            sb.Append("  $(\".tablerow\").each(function (i) {");
            sb.Append(" if (i >= begin && i < end)");
            sb.Append(" { $(this).show(); }");
            sb.Append(" });  return false; }");

            return sb.ToString();
        }
        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var url =\"{0}\";", url);
            sb.AppendFormat("var notificationYes = \"<!--{0}-->\";", NotificationData.YES);
            sb.Append("   var Del = function (id, trid) {");
            sb.Append(" if (confirm(\"你确定要删除该用户吗？\")) {");
            sb.Append("var deleteurl = url + \"/\" + id;");
            sb.Append("  $.get(deleteurl, null, function (result) { ");
            sb.Append(" if (contains(result, notificationYes, true)) {");
            sb.Append("  $(\"#\" + trid).remove();");
            sb.Append("}");
            sb.Append("$(\"#content_box_messages\").html(result);");
            sb.Append(" $(\".close\").click(");
            sb.Append("function () {");
            sb.Append(" $(this).fadeTo(400, 0, function () {");
            sb.Append("$(this).slideUp(400);");
            sb.Append("});  return false;});");
            sb.Append("     });    }  };");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                style.attr(type: MimeType.text.css)[" .selectedpagelabel  {    cursor: default;    text-decoration: none;   }"],
                  script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery.validate.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                script.attr(type: MimeType.text.javascript)[Script1(Model)],
                h2[Model.Title],
                p.attr(id: "page-intro")["  列表系统中所有的安全用户，您可以删除这些用户，但请谨慎，因为关联的用户授权及用户角色也将删除。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3["用户列表"],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["所有"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id: "content_box_messages"),
                                table.attr(id: "UserList_Table")
                                [
                                     thead.attr(id: "UserList_Table_Thead")
                                     [
                                        tr[th["用户名"], th["昵称"], th["邮箱"], th["角色值"], th["状态值"], th["操作"]]
                                     ],
                                     tbody.attr(id: "UserList_Table_Tbody")
                                     [
                                        ForEach(this.Model.Users, (item, c) => c
                                            [
                                                   tr.attr(@class: "tablerow userlist", id: "tr_" + item.UserID)
                                                   [
                                                      th[item.UserName],
                                                      td[item.NickName],
                                                      td[item.EMail],                                                      
                                                      td[item.RoleValue],
                                                      td[item.State],
                                                      td[
                                                           a.attr(href: Url.RouteUrl("SecurityApp_User", new { action = "AuthorizeUser" })+"/"+item.UserID)["授权"],
                                                           "&nbsp;&nbsp;",
                                                         a.attr(href: Url.RouteUrl("SecurityApp_User", new { action = "ResetPassword" }) + "/" + item.UserID)["重置密码"],
                                                          "&nbsp;&nbsp;",
                                                          a.attr(href: "#", onclick: "Del('" + item.UserID + "','tr_" + item.UserID + "');")["删除"]
                                                      ]
                                                   ]
                                            ])
                                     ],
                                     tfoot.attr(id:"UserList_Table_Tfoot")
                                     [
                                        tr[
                                              td.attr(colspan: 6)[div.attr(id: "pagelabel", @class: "pagination")]
                                           ]
                                     ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
